package com.linkage.jz.bankruptcy.adapter.Vote;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVoters;
import com.linkage.seapeak.utils.CommonUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ProVoteVotersCheckAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<ProVoteVoters> mData;

    public ProVoteVotersCheckAdapter(Context context, List<ProVoteVoters> mData) {
        this.context = context;
        this.mData = mData;
        if(mData != null) {
            for (int i = 0; i < mData.size(); i++) {
                //设置默认的显示
                mData.get(i).setChecked(true);
            }
        }
    }

    //更改集合内部存储的状态
    private void initCheck(boolean checked) {
        if(mData != null) {
            for (int i = 0; i < mData.size(); i++) {
                //更改
                mData.get(i).setChecked(checked);
            }
        }
    }

    public void selectAll(){
        initCheck(true);
        notifyDataSetChanged();
    }
    public void unSelectAll(){
        initCheck(false);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_pro_vote_voters_check, parent, false);
        RecyclerView.ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
        final ProVoteVoters item = mData.get(position);
        final ViewHolder holder = (ViewHolder)viewHolder;
       /* Glide.with(context).load(holder.avatar)
                .placeholder(R.drawable.avatar_defult)//图片加载出来前，显示的图片
                .error(R.drawable.avatar_defult)//图片加载失败后，显示的图片
                .centerCrop().into(holder.avatar);*/
        holder.avatar.setImageResource(R.drawable.avatar_defult);
        holder.name.setText(item.getName());
        holder.sureMoney.setText("确认金额：" + CommonUtils.resetMoney(item.getSureMoney()) + "元");
        holder.voteMoney.setText("表决金额：" + CommonUtils.resetMoney(item.getVoteMoney()) + "元");
        holder.voteCode.setText("参会编号：" + item.getId());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //用集合保存当前的状态
                mData.get(position).setChecked(isChecked);
            }
        });
        holder.checkBox.setChecked(item.isChecked());
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private CheckBox checkBox;
        private CircleImageView avatar;
        private TextView name;
        private TextView sureMoney;
        private TextView voteMoney;
        private TextView changeBtn;
        private TextView voteCode;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.voter_check);
            avatar = itemView.findViewById(R.id.voter_avatar);
            name = itemView.findViewById(R.id.voter_name);
            sureMoney = itemView.findViewById(R.id.voter_sure_money);
            voteMoney = itemView.findViewById(R.id.voter_vote_money);
            changeBtn = itemView.findViewById(R.id.voter_change);
            voteCode = itemView.findViewById(R.id.voter_code);
        }
    }
}
